﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using System.Web.UI;

using DevExpress.Web.ASPxHeadline;

using LMD.Lib.DataClass;
using LMD.Constant;
using LMD.Lib.Interface;
using LMD.Lib.Oracle;
using LMD.Lib.Controls;

/// <summary>
/// Summary description for CCManager
/// </summary>
public static class CCManager
{
    public static String BuildEditor_CCData(this CCData ccData)
    {    
        String sxOutput = String.Empty;

        String sxOTFComboCustomer = CustomControls.Create_OTFComboBox( 
            KCC.CUSTOMER_ID, KLOV.CourtesyCall.COMPANYLIST, null, null, TextboxType.Empty, String.Empty, "SelectCompany()");
        String sxOTFComboContact = CustomControls.Create_OTFComboBox(KCC.CP_ID, KLOV.Generic.CONTACTPERSON, null, null, TextboxType.Empty, "-1", String.Empty);
        String sxDateControl = CustomControls.Create_TextBox(KCC.PLANNED_DATE, KControl.INPUT_ID,
            null, true, TextboxType.DateTime);
        Dictionary<String, String> dicOption = new Dictionary<String, String>();
        dicOption.Add("PLANNED","PLANNED");
        dicOption.Add("DIRECT", "UNPLANNED");
        String sxComboType = CustomControls.Create_ComboBox(KCC.CALL_TYPE, KControl.INPUT_ID, dicOption, "PLANNED");

        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Name", sxOTFComboCustomer);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Contact Person", sxOTFComboContact);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Planned Date", sxDateControl);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Call Type", sxComboType);        


        return sxOutput;
    }
    public static String BuildEditor_UpdateCCData(this CCData ccData)
    {
        String sxOutput = String.Empty;

        String sxDateControl = CustomControls.Create_TextBox(KCC.REALIZATION_DATE, KControl.INPUT_ID,
            null, true, TextboxType.DateTime);
        
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Realization Date", sxDateControl);
		String sxUploadControl = CustomControls.BuildEditor_FileUpload(KCC.FILEUPLOADID,
            KFileHandler.CC.COURTESY_CALL, null, KControl.INPUT_ID, TextboxType.FileUpload);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Upload File", sxUploadControl);
        sxOutput += CustomControls.BuildEditor_LabelAndText(KCC.NOTE, KControl.INPUT_ID,
            "Note", null);

        return sxOutput;
    }
    public static String BuildEditor_EditPlannedDate(String sxControlId, int iCCID, DateTime dtmPlannedDate)
    {
        String sxOutput = String.Empty;

        String sxDateControl = CustomControls.Create_TextBox(KCC.PLANNED_DATE, KControl.INPUT_ID,
            dtmPlannedDate.ToString(), true, TextboxType.DateTime);

        String sxDef = CustomControls.BuildFieldSet_PairedLabel("Planned Date", sxDateControl);

        sxOutput = String.Format(
            "<div id=\"{1}_{0}\" class=\"popLOV\">" +
                "<fieldset class=\"m_fs_border\" id=\fs_lov_{0}\">" +
                    "<legend>" +
                        "<a class=\"lnk\" href=\"javascript:toggle('lov_{0}');\">Change Planned Date</a>" +
                    "</legend>" +
                    "<dl class=\"m_fs\"><div id=\"div_invite_{0}\">{2}</div></dl>" +
                    "<div id=\"load\" style=\"float: right; padding-right: 10px;\"></div>" +
                    "<input type=\"button\" id=\"btnCancel_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 15px;\"" +
                        " onclick=\"closeEditPlannedDate();\" value=\"cancel\" />" +
                    "<input type=\"button\" id=\"btnSave_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 5px;\"" +
                        " onclick=\"saveEditPlannedDate('{0}');\" value=\"save\" />" +
                    "<div id=\"load_{0}\" style=\"float: right; padding-right: 10px;\"></div>" +
                "</fieldset>" +
            "</div>",
            sxControlId,
            KControl.GENERICPOPFORM_ID,
            sxDef
            );       
        return sxOutput;
    }
    public static String BuildFieldSet_CCData(this CCData ccData)
    {
        String sxOutput = String.Empty;

        String cpInfo = String.Empty;
        int icpID = -1;
        String isNewCP = String.Empty; ;
        if (ccData.iContactPersonID > 0)
        {
            icpID = ccData.iContactPersonID;
            isNewCP = "N";
        }
        else
        {
            icpID = ccData.iNewContactPersonID;
            isNewCP = "Y";
        }

        cpInfo = String.Format("&nbsp;<a class=\"lnk\" href=\"javascript:ShowPopUp('{0}','{1}');\">(Full Info)</a>",
            icpID.ToString(), isNewCP);

        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Company Name", ccData.sxCompanyName);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Contact Person", ccData.sxContactPersonName+cpInfo);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Planned Date", ccData.dtmPlannedDate.ToString("dd-MMM-yyyy HH:mm:ss"));
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Call Status", ccData.sxCallStatus);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Create By", ccData.sxCreateBy);
        
        if(ccData.dtmRealizationDate != DateTime.MinValue)
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Realization Date", ccData.dtmRealizationDate.ToString("dd-MMM-yyyy HH:mm:ss"));
        }
		if (ccData.iFileUploadId > 0)
        {
            String sxLinkAttachment = CustomControls.Create_LinkDiv(KCC.FILEUPLOADID,
            "downloadFile('" + ccData.iFileUploadId.ToString() + "');"
            , ccData.sxFileName);
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("File Uploaded", sxLinkAttachment);
        }
        if(ccData.sxNote != String.Empty)
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Note", ccData.sxNote);
        }
        if(ccData.sxCallStatus.Equals("CANCELED"))
        {
            sxOutput += CustomControls.BuildFieldSet_PairedLabel("Cancelation Reason", ccData.sxReason);
        }
        
        return sxOutput;
    }

    public static String BuildEditor_CCDetail(this DataTable dtQuestion)
    {
        String sxOutput = String.Empty;

        for(int i = 0; i < dtQuestion.Rows.Count; i++)
        {

            int iInputTypeId = Convert.ToInt32(dtQuestion.Rows[i]["P_INPUT_CONTROL_TYPE_ID"]);
            TextboxType tpTextbox = (TextboxType)iInputTypeId;
            String sxControlId = dtQuestion.Rows[i]["P_QUESTION_TYPE_ID"].ToString();
            String sxQuestion = dtQuestion.Rows[i]["CODE"].ToString();
            bool isMandatory = dtQuestion.Rows[i]["IS_MANDATORY"].ToString().Equals(KGlobal.YES);
            sxOutput += CustomControls.BuildEditor_LabelAndText(sxControlId,
                KControl.INPUT_ID, sxQuestion, null, tpTextbox, isMandatory, String.Empty);
        }

        return sxOutput;
    }

    public static String BuildFieldSet_CCQuestionDetail(this DataTable dtQuestion)
    {
        String sxOutput = String.Empty;
        
        for (int i = 0; i < dtQuestion.Rows.Count; i++)
        {
            switch (dtQuestion.Rows[i]["CODE_TYPE"].ToString())
            {
                case "RATING":
                    sxOutput += CustomControls.BuildFieldSet_PairedLabel(
                        dtQuestion.Rows[i]["QUESTION"].ToString(), dtQuestion.Rows[i]["VALUE"].ToString());
                    sxOutput += CustomControls.BuildFieldSet_PairedLabel(
                        "", dtQuestion.Rows[i]["NOTE"].ToString());
                    break;
                case "YESNOOPTION":
                    sxOutput += CustomControls.BuildFieldSet_PairedLabel(
                        dtQuestion.Rows[i]["QUESTION"].ToString(), dtQuestion.Rows[i]["VALUE"].ToString());
                    sxOutput += CustomControls.BuildFieldSet_PairedLabel(
                        "", dtQuestion.Rows[i]["NOTE"].ToString());
                    break;
                default:
                    sxOutput += CustomControls.BuildFieldSet_PairedLabel(
                        dtQuestion.Rows[i]["QUESTION"].ToString(), dtQuestion.Rows[i]["VALUE"].ToString());
                    break;
            }
        }

        return sxOutput;
    }

    public static String BuildReminderLink(DataTable dtReminder, bool bSeeAll)
    {
        String sxOutput = String.Empty;
        
        foreach (DataRow dr in dtReminder.Rows)
        {
            
            sxOutput += String.Format(
            "<table cellspacing=\"0\" cellpadding=\"0\" id=\"{0}\" border=\"0\" class=\"rmT\">" +
                "<tr>" +
                    "<td valign=\"top\"" +
                        "<a class=\"rmH\" href=\"CCReal.aspx?ccid={0}\">{1}</a>" +
                    "</td>" +
                "</tr>" +
                "<tr>" +
                    "<td valign=\"top\">" +
                        "<div class=\"rmCP\">Contact Person : {2}</div>" +
                        "<br/>" +
                        "<div class=\"rmD\">Planned Date: {3}</div>" +
                    "</td>" +
                "</tr>" +
            "</table>",
            dr["T_COURTESY_CALL_ID"].ToString(),
            dr["COMPANY_NAME"].ToString(),
            dr["NAME"].ToString(),
            Convert.ToDateTime(dr["PLANNED_DATE"]).ToString("dd-MMM-yyyy HH:mm:ss")
            );
        }
        if (!bSeeAll)
        {
            sxOutput += "<a class=\"lnk\" href=\"CCReminder.aspx\">See All Reminder</a>";
        }
        
        return sxOutput;
    }
    public static String BuildEditor_CPInfoPopUp(String sxControlId, ContactInfoData cpInfoData)
    {
        String sxOutput = String.Empty;

        String sxDef = String.Empty;

        sxDef += CustomControls.BuildFieldSet_PairedLabel("Contact Person", cpInfoData.sxContactName);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Job Position", cpInfoData.sxJobPositionName);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Phone Number", cpInfoData.sxPhoneNumber);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Cell Phone Number", cpInfoData.sxCellPhoneNumber);
        sxDef += CustomControls.BuildFieldSet_PairedLabel("Email Address", cpInfoData.sxEmailAddress);


        sxOutput = String.Format(
            "<div id=\"{1}_{0}\" class=\"popLOV\">" +
                "<fieldset class=\"m_fs_border\" id=\fs_lov_{0}\">" +
                    "<legend>" +
                        "<a class=\"lnk\" href=\"javascript:toggle('lov_{0}');\">Contact Person Info</a>" +
                    "</legend>" +
                    "<dl class=\"m_fs\"><div id=\"div_invite_{0}\">{2}</div></dl>" +
                    "<div id=\"load\" style=\"float: right; padding-right: 10px;\"></div>" +
                    "<input type=\"button\" id=\"btnCancel_lov_{0}\" class=\"btn\" style=\"float: right; margin-left: 5px; margin-right: 15px;\"" +
                        " onclick=\"closeCPInfo();\" value=\"cancel\" />" +
                "</fieldset>" +
            "</div>",
            sxControlId,
            KControl.GENERICPOPFORM_ID,
            sxDef
            );
        return sxOutput;
    }
    public static String BuildEditor_CCSummary()
    {
        String sxOutput = String.Empty;

        String sxOTFCAR = CustomControls.Create_OTFComboBox(KCC.CARID,
            KLOV.Generic.ICPCAR, "", "", TextboxType.Empty, String.Empty, String.Empty);
        String sxStartDate = CustomControls.Create_TextBox(KCC.START,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        String sxEndDate = CustomControls.Create_TextBox(KCC.END,
            KControl.INPUT_ID, String.Empty, true, TextboxType.Date);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("CAR", sxOTFCAR);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("Start Date", sxStartDate);
        sxOutput += CustomControls.BuildFieldSet_PairedLabel("End Date", sxEndDate);
        sxOutput +=
            "<dt>&nbsp;</dt><dd><input type=\"button\" id=\"btnFilter\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"Filter();\" value=\"FILTER\" />" +
            "<input type=\"button\" id=\"btnAll\" class=\"btn\"" +
            "style=\"margin-left: 5px; margin-right: 5px; width:100px;\"" +
                "onclick=\"ShowAll();\" value=\"SHOW ALL\" /></dd> " +
            "<div id=\"load\" style=\"float: right; margin-left: 5px; margin-right: 5px;\"></div>";
        return sxOutput;
    }
}
